<template>
	<view class="main">
		<view class="tit">
			<text>待授权主体列表：</text>
			<div style="display: flex;">
				<button class="btn2" type="primary" @click="lookInv">查看调查表</button>
				<button class="btn" type="primary" @click="getList">刷新</button>	
			</div>
		</view>
		<view class="table">
			<view class="th">
				<view class="thCont" v-for="item in th" :key='item.name'>
					{{item.name}}
				</view>
			</view>
			<block v-if="td.length==0">
				<view class="noList" >暂无数据</view>
			</block>
			<block v-for="(item,index) in td">
				<view class="td">
					<view class="tdCont">
						<text> {{index+1}}</text>
					</view>
					<view class="tdCont">
						{{item.customerName}}
					</view>
					<view class="tdCont">
						{{item.entityRoleTypeName}}
					</view>
					<view class="tdCont">
						{{item.isAuth}}
					</view>
					<view class="tdCont">
						<button v-if="item.tobeAuthType!=='3' && null!==item.tobeAuthType" class="btn" @click="qcode(item)" type="primary">二维码</button>
						<button class="btn" :disabled="item.isAuth=='未授权'" @click="look(item)" type="primary">征信报告</button>
					</view>
				</view>
			</block>
			
		</view>
		<block >
			<view class="tit">
				<text>集团成员校验列表：</text>
			</view>
			<view class="table">
				<view class="th">
					<view class="thCont" v-for="item in th2" :key='item.name'>
						{{item.name}}
					</view>
				</view>
				<block v-if="td2.length==0">
					<view class="noList" >暂无数据</view>
				</block>
				<block v-for="(item,index) in td2">
					<view class="td">
						<view class="tdCont">
							<text> {{index+1}}</text>
						</view>
						<view class="tdCont">
							{{item.custName}}
						</view>
						<view class="tdCont">
							{{item.custTypeName}}
						</view>
						<view class="tdCont">
							{{item.companyName}}
						</view>
					</view>
				</block>
				
			</view>
			
				<view class="tit">
					说明：
				</view> 
			
			<!-- input -->
			<ActiveForm   style="width: 100% !important;">
				
				<view class="active_input">
					<textarea maxlength="1000" placeholder='最多输入1000字' v-model="descStr" auto-height style="min-height: 20rem;width: 100%;font-size: 2.6rem;" />
				</view>
			</ActiveForm>
		</block>

		<button :disabled="disabled" type="primary" size="mini" class="submit" @click="submit">提交</button>


		<!-- 弹框 -->
		<popup ref='popup' type="info" :showCancel="false" @confirm='pclick'>
			<view class="qcode">
				<text style="font-size: 2.8rem;">请使用手机自带浏览器扫描</text>
				<tki-qrcode :size='400' ref="qrcode" :val="val" :showLoading="true" style="width: 100%;height: 100%;" />
			</view>
		</popup>
		<uni-popup ref="popups" type="dialog" style="width: 70rem;height: 40rem;">
			<uni-popup-dialog @confirm='zxbgcon' title="征信报告" type="info" :duration="2000" style="width: 100%;height: 100%;">
				<scroll-view scroll-y="true" style="height: 70rem;">
					<view class="zx" v-append="zxbg" @appended="appended"></view>
				</scroll-view>
			</uni-popup-dialog>
		</uni-popup>

	</view>
</template>

<script>
	import tkiQrcode from "@/components/tki-qrcode/tki-qrcode.vue"
	import uniPopupDialog from "@/components/uni-popup-dialog/uni-popup-dialog.vue";
	export default {
		data() {
			return {
				base:`http://10.2.224.50:8099/creditQuery/`,
				disabled:false,
				zxbg: '',
				val: '',
				th: [{
						name: '序号'
					},
					{
						name: '名称'
					},
					{
						name: '与申请人关系'
					},
					{
						name: '状态'
					},
					{
						name: '操作'
					}
				],
				th2: [{
						name: '序号'
					},
					{
						name: '客户名称'
					},
					{
						name: '客户类型'
					},
					{
						name: '疑似集团名称'
					},
				],
				td: [
					// {name:'张三',type:'父子',state:'待调查'},
					// {name:'张三',type:'父子',state:'待调查'}
				],
				td2: [],
				descStr:''
			}
		},

		onBackPress(options) {

			console.log(options)
			if (options.from === 'backbutton') {
				getApp().globalData.isBack = true
			}

		},
		onUnload() {
			//console.log(123)  
		},
		components: {
			tkiQrcode
		},
		mounted() {
			this.zxbg = ``
			this.getList()
			this.getComList()  //集团成员接口，6月底上线
			
			window.addEventListener("popstate", function(e) {
				getApp().globalData.isBack = true //根据自己的需求实现自己的功能 
			}, false);
			//this.getBase()
		},
		methods: {
			lookInv(){
				uni.navigateTo({
					url:"/pages/authorizedList/invExcel"
				})
				
			},
			checkStatus(){
				return new Promise((resolve,reject)=>{
					this.sendRequest({
							url: 'loanBaseInfo/checkAllAuthCompletedOrNot/'+ getApp().globalData.reqId,
							method: 'get',
							hideLoading: false,
							complete: (res) => {
								resolve(res.data)
							}
						})

				})

			},
			
			async submit() {
				let flag = 0
				try {
					// this.td.map((item, key) => {
					// 	if (item.isAuth == '未授权') {
					// 		flag++
					// 		uni.showToast({
					// 			icon: "none",
					// 			title: "有未授权人员，请完成授权！",
					// 			duration: 2000
					// 		})
					// 		return false
					// 	}

					// })
					let status = await this.checkStatus()
					if(status==='N'){
						uni.showToast({
								icon: "none",
								title: "有未授权人员，请完成授权！",
								duration: 2000
							})
						flag++
						return false
					}
				
				} catch {

				} finally {
					//console.log(flag)
					if (flag == 0) {
						//集团功能未上线
						if (this.descStr == '') {
							uni.showToast({
								icon: "none",
								title: "备注说明不能为空！",
								duration: 2000
							})
							return false
						}
						
						// /businesLocation/validLocationByReqId   确认定位判断 0415需求
						this.sendRequest({
							url: 'businessLocation/validLocationByReqId',
							method: 'post',
							hideLoading: false,
							data: {
								reqId: getApp().globalData.reqId,
							},
							complete: (resFlag) => {
								console.log(resFlag)
								if (resFlag.code == '200') {
									if(resFlag.data.isPass==='1'){
									   this.checkCus()
									}else{
										uni.showToast({
											icon: "none",
											title: "尚未确认【调查地点】，无法提交，请返回“调查资料”界面确认调查地点",
											duration: 2000
										})
										
									}
									
								}else{
									uni.showToast({
										icon: "none",
										title: "尚未确认【调查地点】，无法提交，请返回“调查资料”界面确认调查地点",
										duration: 2000
									})
								}
							}
						})
						
						
					}
				}

			},
			checkCus(){
				let that = this
				that.sendRequest({
					url: 'bbwLoanApproved/getStockAtypismCustomerManager/'+ getApp().globalData.reqId,
					method: 'get',
					hideLoading: false,
					complete: (res) => {
						if (res.code == '200') {
							if(res.data){
								uni.showModal({
									title: '提示',
									content:'【'+res.data+'】在数字小微系统内有其他未结清业务，且管户客户经理与当前业务不一致，若办理本笔业务，需移交或结清该存量业务，是否继续？',
									cancelText: '取消',
									confirmText: '确定',
									success(res) {
										if (res.confirm) {
								            that.submitForm()
										}else if (res.cancel) {
											uni.navigateBack({
												delta: 1
											});
			
		                                }
									}
								})
								
							}else{
								that.submitForm()
							}
							
						}else{
							uni.showToast({
								icon: "none",
								title: "校验失败，请重试",
								duration: 2000
							})
						}
					}
				})
				
				
			},
			submitForm(){
				this.sendRequest({
					url: 'bbwLoan/befor/userSubmit',
					method: 'post',
					hideLoading: false,
					data: {
						reqId: getApp().globalData.reqId,
						descStr:this.descStr,
						groupList:this.td2
					},
					complete: (res) => {
						console.log(res)
						if (res.code == '200') {
							this.disabled = true
							uni.reLaunch({
							    url: '/pages/authorizedList/success'
							});	
						}else{
							uni.showToast({
								icon: "none",
								title: "提交失败，请重试",
								duration: 2000
							})
						}
					}
				})
				
			},
			getList() {
				this.sendRequest({
					url: 'loanBaseInfo/queryTobeAuthorization',
					method: 'post',
					hideLoading: false,
					data: {
						reqId: getApp().globalData.reqId
					},
					complete: (res) => {

						if (res.code == '200') {
							this.td = res.data
						}
					}
				})
			},
			//获取集团成员列表
			getComList(){
				this.sendRequest({
					url: 'loanBaseInfo/getGroupMemberList',
					method: 'post',
					hideLoading: false,
					data: {
						reqId: getApp().globalData.reqId
					},
					complete: (res) => {
				
						if (res.code == '200') {
							if(!res.data.groupList){
								this.td2 = []
							}else{
								this.td2 = res.data.groupList
							}
							
						}
					}
				})
			},
			getBase(){
				this.sendRequest({
					url: 'offsurCredit/jsAndCssUrl',
					method: 'post',
					hideLoading: false,
					data: {
						reqId: getApp().globalData.reqId
					},
					complete: (res) => {
				
						if (res.code == '200') {
							this.base = res.data
						}
					}
				})
			},
			appended() {

				// could use jQuery 
				//alert(window.jQuery);
			},
			qcode(item) {
				this.val = getApp().globalData.toUrl + '?reqId=' + item.reqId + '&&tobeId=' + item.id + '&&tobeAuthType=' + item.tobeAuthType +
					'&&customerType=' + item.customerType + '&&entityRoleType=' + item.entityRoleType + '&&customerName=' + item.customerName
				this.$refs.popup.open('', '授权二维码')
				console.warn(this.val)
				setTimeout(() => {
					this.$refs.qrcode._makeCode()
				}, 500)

			},
			pclick(item) {
				this.$refs.qrcode._clearCode()

			},
			zxbgcon() {

				this.$refs.popups.close()
			},
			look(item) {
                let queryParams = {
                    'reqID': item.reqId,
                    'customerType': item.customerType,
                    'customerSystemId': item.customerSystemId,
                    'customerName': item.customerName,
                    'customerCertificateNumber': item.customerCertificateNumber,
                    'type': 'pad',
                    'queryType': '01'
                }
                console.log('征信报告查询参数:', queryParams)
				this.sendRequest({
					url: 'offsur/query/subjectData',
					method: 'post',
					hideLoading: false,
					data: queryParams,
					complete: (res2) => {
						console.log(res2)
						if (res2.code == '200') {
							if (!res2.data) {
								uni.showToast({
									icon: "none",
									title: '获取失败，请重试',
									duration: 2000
								})
							} else {
								this.$refs.popups.open()
								let html =res2.data.credit.htmlMessage
								this.zxbg = html
							}				
						} else {
							uni.showToast({
								icon: "none",
								title: res2.message,
								duration: 2000
							})
						}
					}
				})
			}
		}
	}
</script>
<style scoped>

</style>
<style lang="scss">
	.qcode {
		width: 60rem;
		height: 60rem;
		text-align: center;

		uni-image {
			width: 100%;
			height: 100%;
		}

		image {
			width: 100%;
			height: 100%;
		}
	}

	.zx {
		width: 200rem;
		height: 100rem;
	}

	.main {
		padding: 5rem;

		.tit {
			width: 100%;
			font-size: 3.5rem;
			font-weight: bold;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 1rem;
			

			.btn {
				font-size: 2.8rem;
				width: 18rem;
				margin: 0;
				padding: 0;
			}
			.btn2 {
				font-size: 2.8rem;
				width: 30rem;
				margin: 0;
				padding: 0;
				margin-right: 2rem;
			}
		}

		.table {
			width: 100%;
			font-size: 3rem;
			margin-bottom: 4rem;

			.th {
				background: #EAF0FF;
				border: 1px solid #dddddd;
				display: flex;
				justify-content: space-around;
				text-align: center;

				.thCont {
					width: 25%;
					padding: 1rem 2rem;
				}
			}

			.td {
				width: 100%;
				border: 1px solid #dddddd;
				display: flex;
				justify-content: space-around;
				text-align: center;

				.tdCont {
					width: 25%;
					padding: 1rem 2rem;
					display: flex;
					align-items: center;
					justify-content: center;

					.btn {
						font-size: 2.8rem;
						width: 18rem;
						padding: 0;
					}
				}
			}
		}
	}

	.submit {
		width: 40rem;
		margin-top: 5rem;
		padding: 0;
	}
	.uni-border-left{
		border: none !important;
	}
	.noList{
		color: #ccc;
		text-align: center;
		border: 1rpx solid #ddd;
		border-top: none;
		padding: 2rem;
	}
</style>
